package com.loan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.utils.StringUtils;
import com.loan.system.domain.ChannelEntity;
import com.loan.system.domain.req.ConfigEntityReq;
import com.loan.system.mapper.ChannelMapper;
import com.loan.system.service.AppConfigService;
import com.loan.system.service.ChannelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ChannelServiceImpl implements ChannelService {

    private final ChannelMapper channelMapper;

    private final AppConfigService appConfigService;

    @Override
    public TableDataInfo<ChannelEntity> selectPageList(ChannelEntity entity, PageQuery pageQuery) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<ChannelEntity> lqw = new LambdaQueryWrapper<ChannelEntity>()
                .like(StringUtils.isNotBlank(entity.getName()),ChannelEntity::getName, entity.getName())
                .like(StringUtils.isNotBlank(entity.getCode()),ChannelEntity::getCode, entity.getCode())
                .eq(ChannelEntity::getDelFlag,"0")
                .orderByAsc(ChannelEntity::getId);
        Page<ChannelEntity> page = channelMapper.selectPage(pageQuery.build(), lqw);
        //获取系统配置
        ConfigEntityReq configEntityReq = appConfigService.getDetail();
        if (StringUtils.isNotBlank(configEntityReq.getFrontUrl())){
            for (ChannelEntity channelEntity: page.getRecords()){
                channelEntity.setUrl(configEntityReq.getFrontUrl() + "?channel=" + channelEntity.getCode());
            }
        }
        return TableDataInfo.build(page);
    }

    @Override
    public List<ChannelEntity> getList() {
        return channelMapper.selectList(new QueryWrapper<ChannelEntity>().eq("del_flag","0").orderByDesc("id"));
    }

    @Override
    public ChannelEntity selectChannelById(Long id) {
        return channelMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addChannel(ChannelEntity entity) {
        return channelMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateChannel(ChannelEntity entity) {
        return channelMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteChannelById(Long id) {
        return channelMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStatus(ChannelEntity entity) {
        return channelMapper.updateById(entity);
    }
}
